﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Messaging;
using System.ServiceModel;
using System.Threading;
using System.Transactions;
using System.Windows;
using System.Windows.Threading;
using WCFClientService;
using WCFIServices;

namespace ClientApplicationOne
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        static PushMessageService pushMessageService = null;
        static ServiceHost pushMessageServiceHost = null;

        public static readonly DependencyProperty EmployeeListProperty =
            DependencyProperty.Register("EmployeeList", typeof (List<Employee>), typeof (MainWindow), new PropertyMetadata(new List<Employee>()));

        public List<Employee> EmployeeList
        {
            get { return (List<Employee>) GetValue(EmployeeListProperty); }
            set { SetValue(EmployeeListProperty, value); }
        }

        public MainWindow()
        {
            InitializeComponent();
            IntializeConnection();
        }

        private void IntializeConnection()
        {
            if (pushMessageServiceHost == null)
            {
                string messageBoxMSMQName = ConfigurationManager.AppSettings["MessageBoxMSMQName"];

                if (!MessageQueue.Exists(messageBoxMSMQName))
                {
                    MessageQueue versionManagerQueue = MessageQueue.Create(messageBoxMSMQName, true);
                    versionManagerQueue.SetPermissions(@"Everyone", MessageQueueAccessRights.FullControl);

                    versionManagerQueue.SetPermissions(@"ANONYMOUS LOGON",
                        MessageQueueAccessRights.ReceiveMessage |
                        MessageQueueAccessRights.PeekMessage |
                        MessageQueueAccessRights.WriteMessage);
                }

                pushMessageService = new PushMessageService();
                pushMessageServiceHost = new ServiceHost(pushMessageService);

                pushMessageService.PropertyChanged += PropertyChangedEvent;
                pushMessageServiceHost.Open();

                var registerClientServiceClient = new RegisterClientServiceClient("RegisterClientEndPoint");

                using (var scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    var clientInfo = new ClientInfo
                        {
                            GroupName = ConfigurationManager.AppSettings["GroupName"],
                            ClientName = System.Net.Dns.GetHostName()
                        };

                    // ClientName can be changed to ip address
                    clientInfo.PushMessageBoxAddress = "net.msmq://" + clientInfo.ClientName + "/private/WcfServerPush/ClientMessageBox";

                    registerClientServiceClient.RegisterClient(clientInfo);

                    scope.Complete();
                }

             
            }
        }

        void PropertyChangedEvent(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "PushMessageService.Message.Changed")
            {
                try
                {
                    var pushMessageService = (PushMessageService)sender;
                    Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new ThreadStart(() =>
                        {
                            EmployeeList.Add(pushMessageService.Employee);
                            var tempList = EmployeeList;
                            EmployeeList = null;
                            EmployeeList = tempList;
                        }));
                    
                  //  textBoxClient.Text = pushMessageService.Message;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }
}
